from Tkinter import *
from math import sqrt, atan2, pi

SIZE=600
SIZE2 =SIZE//2


BULL      =  127*SIZE//10000
TWENTY5   =  318*SIZE//10000
SINGLE1   =  990*SIZE//10000
TREBLE    = 1070*SIZE//10000
SINGLE2   = 1620*SIZE//10000
DOUBLE    = 1700*SIZE//10000
OUTER     = 3400*SIZE//10000

def to_mm(pix):
    return OUTER*pix/SIZE2

def to_pix(mm):
    return mm*SIZE2/OUTER

KEY= 20,1,18,4,13,6,10,15,2,17,3,19,7,16,8,11,14,9,12,5
EDGE=6.35, 15.9, 99, 107, 162, 170, 226.5
OUTER=EDGE[-1]
EDGEPIX = [to_pix(e) for e in EDGE]

for e in EDGE:
    print e, to_pix(e), to_mm(to_pix(e))

class Main(Tk):
    def __init__(self):
        Tk.__init__(self)
        c = self.board = Canvas(self, height=SIZE, width=SIZE)
        c.bind('<Motion>', self.motion)
        c.pack(fill=BOTH, expand=YES)
        for i,e in enumerate(reversed(EDGEPIX)):
            c.create_oval(SIZE2-e, SIZE2-e, SIZE2+e, SIZE2+e, fill=('red' if i%2 else 'green'),width=3)
        for theta in range (0,360,360//20):
            print theta-360//40
        self.mainloop()
    def motion(self, event):
        x,y =event.x-SIZE2, SIZE2-event.y
        r = sqrt(x*x+y*y)
        if r<EDGEPIX[0]:
            where='bull'
        elif r<EDGEPIX[1]:
            where='25'
        elif r<EDGEPIX[2]:
            where='single1'
        elif r<EDGEPIX[3]:
            where='treble'
        elif r<EDGEPIX[4]:
            where='single2'
        elif r<EDGEPIX[5]:
            where='double'
        else:
            where='miss'
        theta = atan2(x, y)*180/pi
        if theta<0:
            theta+=360
        if theta<=8:
            n= 20
        elif theta<=16:
            n= 1
        elif theta<=24:
            n= 18
        elif theta<=32:
            n= 4
        elif theta<=40:
            n= 13
        else:
            n=99
        print x,y,r, theta, where,n

if __name__=='__main__':
    Main()
